package com.ThreadAsist;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
//栅栏
//加法器 线程执行到达 指定数量 执行其他任务线程
//阻塞子线程  +++++++++
public class CyclicBarrierDemo {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
            System.out.println("线程任务达标，准备执行下阶段");
        });
        for (int i = 0; i < 7; i++) {
            final int emp = i;
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"执行，第"+emp+"个线程");
                try {
                    // +1 等待线程执行数到达7
                    cyclicBarrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    e.printStackTrace();
                }

            }).start();
        }

    }
}
